package com.maochang.controller;

import com.maochang.pojo.PsUser;
import com.maochang.pojo.util.Result;
import com.maochang.service.PsUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/GeRenZhoNgXin")
public class GenRenZhoNgXin {

    @Resource
    private PsUserService psUserServiceImpl;

    @GetMapping("/GeRenZiLiAoTZ")
    private ModelAndView GeRenXi(){
        return  new ModelAndView("geRenZhongXin/person");
    }

    @PostMapping("/GaiMiMa")
    private Result chaJiuMiMa(@RequestBody PsUser psUser, HttpSession session,@RequestParam("JiuMiMa") String aa){
        Result  jg=new Result();
        Boolean Falg=false;
        PsUser psUser1= (PsUser) session.getAttribute("loginUser");
        //前端密码输入    转成string
        String encodedPassword = new SimpleHash("md5",psUser.getUserPassword(),"qwert",1).toString();
        //密码为空的一个更改
        if(psUser.getUserPassword().equals("")||psUser.getUserPassword().equals("null")){
            System.out.println("空密码更改");
             psUser.setUserPassword(psUser1.getUserPassword());
             Falg=  psUserServiceImpl.UpdatePwd(psUser);
             if(Falg.equals(true)){
                 jg.setMsg("更改基本信息成功");
             }
        }else {
            //新旧密码相等
               //会话密码                         前端密码旧密码
            System.out.println(psUser1.getUserPassword()+"---"+encodedPassword);
            if(psUser1.getUserPassword().equals(encodedPassword)){
                String encodedPassword2 = new SimpleHash("md5",aa,"qwert",1).toString();
                System.out.println(encodedPassword2+"旧密码解析md");
                psUser.setUserPassword(encodedPassword2);
                Falg=  psUserServiceImpl.UpdatePwd(psUser);
                if(Falg.equals(true)){
                    jg.setMsg("基本信息和密码修改成功");
                }
            } else {
                 jg.setMsg("请正确输入旧密码");
            }
        }
        return  jg;
    };
}
